package com.maxmlr.music.service;

import com.maxmlr.music.dao.PictureMapper;
import com.maxmlr.music.model.Picture;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class PictureService extends FathService {

    @Autowired
    private PictureMapper pictureMapper;

    /**
     * @param order
     * @param pic_category
     * @param page
     * @return
     */
    public List<Picture> findPicture(String order, String pic_category,
                                     Integer page) {
        Map<String, Object> prams = new HashMap<String, Object>();
        prams.put("order", order);
        List<Picture> list = pictureMapper.findByCategory(prams, pic_category,
                page * PICNUM, PICNUM);
        return list;
    }

    /**
     * @param picture
     * @return
     */
    public Picture findPictureBy(Picture picture) {
        if (picture == null) {
            throw new RuntimeException(ERRMSG);
        }
        if (picture.getId() != null) {
            picture = pictureMapper.findById(picture.getId());
        } else if (picture.getPic_category() != null
                && picture.getPic_name() != null) {
            picture = pictureMapper.findByNameAndCategory(
                    picture.getPic_name(), picture.getPic_category());
        }
        return picture;
    }
}
